<template>
  <div class="zui-user-picker">
    <header class="header">
      <span class="cancel">取消</span>
      <h1 class="title">选择人员</h1>
      <span class="confirm">确定</span>
    </header>
    <main class="main">
      <SearchBar v-model="searchText" />
      <SelectedUsers v-if="selectedUsers && selectedUsers.length" class="selected-users-wrap" :users="selectedUsers" />
      <template v-if="!searchText">
        <ZuiField label="分支名称" v-model="searchText" class="entry" />
        <div class="section-title">常用人员</div>
      </template>
      <SearchView
        v-if="searchText"
        :="$props"
        :search-text="searchText"
        :selected-user-ids="selectedUserIds"
        class="search-view"
        @select="onUserSelect"
        @unselect="onUserUnselect"
        @info-click="onUserInfoClick"
      />
    </main>
  </div>
</template>

<script setup>
import { ref, watch, computed, reactive } from 'vue'
import SearchBar from './SearchBar.vue'
import SearchView from './SearchView.vue'
import SelectedUsers from './SelectedUsers.vue'

const props = defineProps({ search: Function, disabledUserIds: Array, rootBranchIds: Array })
const emit = defineEmits(['confirm', 'cancel'])

const selectedUsers = reactive([])
const selectedUserIds = computed(() => {
  console.log('selectedUsers :>> ', selectedUsers)
  if (!selectedUsers || !selectedUsers.length) {
    return
  }
  return selectedUsers.map((u) => u.id)
})
const showSelectedBar = ref(false)

//***** 搜索 *****//
const searchText = ref()
function onUserSelect(user) {
  console.log('selectedUsers :>> ', selectedUsers)
  selectedUsers.push(user)
}
function onUserUnselect(user) {
  const targetIdx = selectedUsers.findIndex((u) => u.id === user.id)
  selectedUsers.splice(targetIdx, 1)
}
function onUserInfoClick(user) {
  console.log('userInfo :>> ', user)
}
</script>

<style lang="scss" scoped>
.zui-user-picker {
  .header {
    display: flex;
    height: 44px;
    align-items: center;
    justify-content: space-between;
    background-color: #ffffff;
    .cancel,
    .confirm {
      padding: 0 20px;
      height: 100%;
      line-height: 44px;
      align-items: flex-start;
      cursor: pointer;
      flex: none;
    }
    .confirm {
      color: var(--zui-color-primary);
    }
    .title {
      text-align: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: var(--zui-size-text-large);
    }
  }
  .main {
    display: flex;
    flex-direction: column;
    min-height: 100px;
    height: calc(100% - 44px);
    background-color: var(--zui-color-background);
    overflow: auto;
    .selected-users-wrap {
      height: 56px;
      border-bottom: 1px solid var(--zui-color-border);
      background-color: #ffffff;
    }
    .entry {
      margin-top: 10px;
    }
    .section-title {
      color: var(--zui-color-text-secondary);
      font-size: var(--zui-size-text-small);
      padding: 0 20px;
      margin-top: 10px;
    }
    .search-view {
      position: relative;
      flex: 1;
      background-color: #ffffff;
      overflow: auto;
    }
  }
}
</style>
